MIT 18.S191 Computational Thinking: Introduction to Images
- What is an image, though?
- How can we store an image in the computer?
- Load image using scikit-image (skimage) package
- Slicing and Indexing
- Manipulating matrices
- Manipulating an image
- Painting a piece of an image
- Modifying the whole image at once
- Transforming an image
Note: This notebook is adapted from MIT 18.S191 Introduction to Computational Thinking course (Fall 2020). The course uses Julia Programming language, and I thought it would be fun to follow along while using Python :).
Note: Check out the lecture video on YouTube
import skimage.io
import numpy as np
import warnings
warnings.filterwarnings("ignore")
url = "https://i.imgur.com/VGPeJ6s.jpg"
philip = skimage.io.imread(url)
skimage.io.imshow(philip)
type(philip)
philip.dtype
philip.shape
# Show the 100th and 400th pixels
skimage.io.imshow(philip[100:101, 400:401, :])
skimage.io.imshow(philip[100, 400].reshape(1, 1, 3))
h, w, _ = philip.shape
# Extract bottom half of the image
# and a tenth of the width until nine tenth of the width into the image
head = philip[(h // 2) : h, (w // 10) : ((9 * w) // 10)]
skimage.io.imshow(head)
head.shape
philip.shape
stacked_philip = np.hstack([head, head])
stacked_philip.shape
skimage.io.imshow(stacked_philip)
a = np.hstack([head, np.flip(head, axis=1)])
b = np.hstack([np.flip(head, axis=0), np.flip(np.flip(head, axis=1), axis=0)])
c = np.vstack([a, b])
a.shape, b.shape, c.shape
skimage.io.imshow(c)
- How can we get inside the image and change it?
- There are two possibilities:
- Modify or mutate numbers inside the array. This is useful when we want to change a small piece
- Create a new copy of the array. This is useful when we want to alter everything together
new_phil = head.copy()
skimage.io.imshow(new_phil)
%%timeit
for i in range(1, 100):
for j in range(1, 300):
new_phil[i, j] = [255, 0, 0] # Paint this corner red
skimage.io.imshow(new_phil)
Using element-wise operations via broadcasting
Instead of using for loops, it is better and more efficient to let NumPy do
the broadcasting for us during arithmetic operations.
See NumPy broadcasting documentation for detailed explanation
new_phil2 = new_phil.copy()
%%timeit
new_phil2[100:200, 1:100] = [0, 255, 0] # Paint this corner green
skimage.io.imshow(new_phil2)
def redify(image):
"""Function that turns a color into just its red component"""
img = image.copy()
img[:, :, 1:] = 0
return img
skimage.io.imshow(redify(new_phil))
def decimate(img, r):
"""Pull every other `r` element in the array"""
return img[0::r, 0::r]
poor_phil = decimate(philip, 5)
poor_phil.shape
skimage.io.imshow(poor_phil)
import skimage.filters
import matplotlib.pyplot as plt
import panel as pn
# Apply Gaussian blur, creating a new image
def blur(image, sigma):
blurred_phil = skimage.filters.gaussian(
image, sigma=(sigma, sigma), multichannel=True
)
return blurred_phil
# Create a widget for exploring different sigma values
sigma = pn.widgets.Player(
name="Sigma", start=1, end=30, value=1, loop_policy="once"
)
@pn.depends(sigma)
def viewer(sigma):
img = blur(image=head, sigma=sigma)
ax = skimage.io.imshow(img)
plt.title(f"Sigma={sigma}")
fig = ax.get_figure()
plt.close(fig)
return fig
p = pn.Column("<br>\n# Image Viewer\n**Select sigma value**", sigma, viewer)
p.save("apps/blur_explorer.html", embed=True)
from IPython.display import HTML, display, display_html
display(HTML("apps/blur_explorer.html"))